package chances.wechat.service.rdb;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import chances.wechat.data.PageBean;
import chances.wechat.entity.UserHistory;
import chances.wechat.service.HistoryService;

@Service("historyService")
public class RdbHistoryService extends BaseDao implements HistoryService {

    @Override
    public void addUserHistory(UserHistory history) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("obj", history);
        try {
            this.insert("insertUserHistory", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<UserHistory> listUserHistories(String userId,
            String contentType, Date beginTime, Date endTime, PageBean page) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);

        if (beginTime != null) {
            paramsMap.put("beginTime", beginTime);
        }

        if (endTime != null) {
            paramsMap.put("endTime", endTime);
        }

        if (StringUtils.isNotBlank(contentType)) {
            paramsMap.put("contentType", contentType);
        }

        try {
            List<UserHistory> result = (List<UserHistory>) this
                    .queryForList("queryUserHistory", paramsMap, 0, page);
            return result;
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<UserHistory> listUserHistories(String userId,
            String contentType, String category, Date beginTime, Date endTime,
            PageBean page) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);

        if (beginTime != null) {
            paramsMap.put("beginTime", beginTime);
        }

        if (endTime != null) {
            paramsMap.put("endTime", endTime);
        }

        if (StringUtils.isNotBlank(contentType)) {
            paramsMap.put("contentType", contentType);
        }

        if (StringUtils.isNotBlank(category)) {
            paramsMap.put("category", category);
        }

        try {
            List<UserHistory> result = (List<UserHistory>) this
                    .queryForList("queryUserHistory", paramsMap, 0, page);
            return result;
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void removeUserHistory(String userId, String contentId) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);
        paramsMap.put("contentId", contentId);
        try {
            this.delete("deleteUserHistory", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void clearUserHistories(String userId, String category) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);

        if (StringUtils.isNotBlank(category)) {
            paramsMap.put("category", category);
        }

        try {
            this.delete("clearUserHistory", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public UserHistory getUserHistory(String userId, String contentId) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);
        paramsMap.put("contentId", contentId);

        try {
            List<UserHistory> result = (List<UserHistory>) this
                    .queryForList("getUserHistory", paramsMap);
            return result.size() > 0 ? result.get(0) : null;
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void dels(String[] codes, String userId) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("codes", codes);
        paramsMap.put("userId", userId);
        try {
            this.delete("deleteUserHistories", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }
}
